using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using PoserLoot.Web.Models;

namespace PoserLoot.Web.Controllers
{
    public class CategoryController : BaseController
    {
        [Authorize(Roles = "Administrators")]
        public ActionResult Delete(Guid id)
        {
            if (_service.DeleteCategory(id))
                return RedirectToAction("Index", "Entry");

            return View();
        }

        [Authorize(Roles = "Administrators")]
        public ActionResult Create()
        {
            ViewData["IEnumerable<Category>"] = _service.ListCategories();
            
            return View();
        }

        [Authorize(Roles = "Administrators")]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(FormCollection collection)
        {
            Category category = new Category()
            {
                Appellation = collection["Appellation"]
            };

            if (_service.CreateCategory(category))
                return RedirectToAction("Index", "Entry");

            ViewData["IEnumerable<Category>"] = _service.ListCategories();
            
            return View(category);
        }

        [Authorize(Roles = "Administrators")]
        public ActionResult Edit(Guid id)
        {
            ViewData["IEnumerable<Category>"] = _service.ListCategories();
            
            return View(_service.GetCategory(id));
        }

        [Authorize(Roles = "Administrators")]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(Guid id, FormCollection collection)
        {
            Category category = _service.GetCategory(id);
            category.Appellation = collection["Appellation"];

            if (_service.EditCategory(category))
                return RedirectToAction("Index", "Entry");

            ViewData["IEnumerable<Category>"] = _service.ListCategories();
            
            return View(category);
        }
    }
}
